package tool

import (
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/request"
	"MoSkeleton/framework/core/dto/response"
	"MoSkeleton/framework/utils"
	"MoSkeleton/global"
	"MoSkeleton/services"
	tool3 "MoSkeleton/web/pogos/request/tool"
	"MoSkeleton/web/v1/http/base"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strings"
)

type GenDatasourceCtrl struct {
	base.BaseCtrl
}

func (d *GenDatasourceCtrl) GetCtrlTitle() string {
	msg, _ := utils.Translater.Translate(consts.TKey_TitleGenDatasourceMsg)
	return msg
}

// 添加数据源
// @Tags 数据源
// @Summary 添加数据源
// @Security ApiKeyAuth
// @Param data body tool3.GenDatasourceAddReq true "添加数据源"
// @Success 200 {object} response.Response{data=tool.GenDatasourceDetailResp,msg=string} "{"code": 200, "data": [...]}"
// @Produce  application/json
// @Router /tool/codegen/datasource [post]
func (d *GenDatasourceCtrl) AddDatasource(c *gin.Context) {
	addReq := new(tool3.GenDatasourceAddReq)
	if err := c.ShouldBindJSON(addReq); err != nil {
		global.MoLogger.Info("参数绑定失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if addReq.DbPasswd != "" {
		srcText, err := utils.Sm4Decrypt(utils.Trans_Sm4Key, addReq.DbPasswd)
		if err != nil {
			global.MoLogger.Error("数据源密码解密失败", zap.Error(err))
			response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_DataSourcePasswdFailMsg)
			return
		}
		addReq.DbPasswd = srcText
	}
	addReq.CreatedBy = utils.GetUsernameFromGin(c)
	addReq.UpdatedBy = addReq.CreatedBy
	if ok, err := services.ServiceGroupInst.ToolGroupInst.GenDatasourceServiceInst.AddDatasource(addReq); err != nil {
		global.MoLogger.Error("添加数据源失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommCreatFailCode, consts.Tkey_DataSourceAddFailMsg)
	} else {
		if !ok {
			response.FailMsgAsI18n(c, consts.CommCreatFailCode, consts.Tkey_DataSourceAddFailMsg)
		} else {
			response.SuccessAsI18n(c, consts.TKey_DataSourceAddSuccessMsg, nil)
		}
	}
}

// 删除数据源
// @Tags 数据源
// @Summary 删除数据源
// @Security ApiKeyAuth
// @Param data body []uint64 true  "删除数据源"
// @Success 200 {object} response.Response{data=string,msg=string} "{"code": 200, "data": [...]}"
// @Produce  application/json
// @Router /tool/codegen/datasource/{ids} [delete]
func (d *GenDatasourceCtrl) DeleteDatasource(c *gin.Context) {
	ids := strings.Split(c.Param("ids"), ",")
	if ok, err := services.ServiceGroupInst.ToolGroupInst.GenDatasourceServiceInst.DeleteDatasource(&ids); err != nil {
		global.MoLogger.Error("删除数据源失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommDeleteFailCode, consts.TKey_DataSourceDeleteFailMsg)
	} else {
		if !ok {
			response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_DataSourceDeleteFailMsg)
		} else {
			response.SuccessAsI18n(c, consts.TKey_DataSourceDeleteSuccessMsg, nil)
		}
	}
}

// 更新数据源
// @Tags 数据源
// @Summary 更新数据源
// @Security ApiKeyAuth
// @Param data body tool3.GenDatasourceUpdateReq true "更新数据源"
// @Success 200 {object} response.Response{data=string,msg=string} "{"code": 200, "data": [...]}"
// @Produce  application/json
// @Router /tool/codegen/datasource [put]
func (d *GenDatasourceCtrl) UpdateDatasource(c *gin.Context) {
	updateReq := new(tool3.GenDatasourceUpdateReq)
	if err := c.ShouldBindJSON(updateReq); err != nil {
		global.MoLogger.Info("参数绑定失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if updateReq.DbPasswd != "" {

		srcText, err := utils.Sm4Decrypt(utils.Trans_Sm4Key, updateReq.DbPasswd)
		if err != nil {
			global.MoLogger.Error("数据源密码解密失败", zap.Error(err))
			response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_DataSourcePasswdFailMsg)
			return
		}
		updateReq.DbPasswd = srcText
	}
	updateReq.UpdatedBy = utils.GetUsernameFromGin(c)
	if ok, err := services.ServiceGroupInst.ToolGroupInst.GenDatasourceServiceInst.UpdateDatasource(updateReq); err != nil {
		response.FailErrorWithCodeAsI18n(c, err, consts.CommUpdateFailCode, consts.TKey_DataSourceUpdateFailMsg)
	} else {
		if !ok {
			response.FailMsgAsI18n(c, consts.CommUpdateFailCode, consts.TKey_DataSourceUpdateFailMsg)
		} else {
			response.SuccessAsI18n(c, consts.TKey_CommUpdateSuccessMsg, nil)
		}
	}
}

// 修改数据源状态
// @Tags 数据源
// @Summary 修改数据源状态
// @Security ApiKeyAuth
// @Param data body request.ChangeStatusReq true "修改数据源状态"
// @Success 200 {object} response.Response{data=string,msg=string} "{"code": 200, "data": [...]}"
// @Produce  application/json
// @Router /tool/codegen/datasource/changeStatus [put]
func (d *GenDatasourceCtrl) ChangeStatus(c *gin.Context) {
	changeStatusReq := new(request.ChangeStatusReq)
	if err := c.ShouldBindJSON(changeStatusReq); err != nil {
		global.MoLogger.Info("参数绑定失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	changeStatusReq.UpdatedBy = utils.GetUsernameFromGin(c)
	if err := services.ServiceGroupInst.ToolGroupInst.GenDatasourceServiceInst.ChangeStatus(changeStatusReq); err != nil {
		global.MoLogger.Error("修改状态失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.CommUpdateFailCode, consts.TKey_CommUpdateStatusFailMsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommUpdateStatusSuccessMsg, nil)
}

// 获取数据源分页列表
// @Tags 数据源
// @Summary 获取数据源分页列表
// @Security ApiKeyAuth
// @Param data body tool3.GenDatasourcePageReq true "获取数据源分页列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "{"code": 200, "data": [...]}"
// @Produce  application/json
// @Router /tool/codegen/datasource/list [get]
func (d *GenDatasourceCtrl) GetDatasourcePage(c *gin.Context) {
	pageReq := new(tool3.GenDatasourcePageReq)
	if err := c.ShouldBindQuery(pageReq); err != nil {
		global.MoLogger.Info("参数绑定失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	pageReq.ShouldBindParams(c)
	pageResult, err := services.ServiceGroupInst.ToolGroupInst.GenDatasourceServiceInst.GetDatasourcePage(pageReq)
	if err != nil {
		global.MoLogger.Error("获取数据源列表失败", zap.Error(err))
		response.FailError(c, err, consts.TKey_DataSourceGetListFailmsg)
		return
	}
	response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, pageResult)
}

// 获取数据源详情
// @Tags 数据源
// @Summary 获取数据源详情
// @Security ApiKeyAuth
// @Param data body uint64 true "获取数据源详情"
// @Success 200 {object} response.Response{data=tool.GenDatasourceDetailResp,msg=string} "{"code": 200, "data": [...]}"
// @Produce  application/json
// @Router /tool/codegen/datasource/{id} [get]
func (d *GenDatasourceCtrl) GetDatasourceById(c *gin.Context) {
	id := utils.StrToUint64Def(c.Param("id"), 0)
	if genDatasourceDetailResp, err := services.ServiceGroupInst.ToolGroupInst.GenDatasourceServiceInst.GetDatasourceById(id); err != nil {
		global.MoLogger.Error("获取数据源信息失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.ServerOccurredErrorCode, consts.TKey_DataSourceGetFailMsg)
	} else {
		if genDatasourceDetailResp.DbPasswd != "" {
			sm4Text, err := utils.Sm4Encrypt(utils.Trans_Sm4Key, genDatasourceDetailResp.DbPasswd)
			if err != nil {
				global.MoLogger.Error("数据源密码加密失败", zap.Error(err))
				response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_DataSourcePasswdFailMsg)
				return
			}
			genDatasourceDetailResp.DbPasswd = sm4Text
		}
		response.SuccessAsI18n(c, consts.TKey_CommGetSuccessMsg, genDatasourceDetailResp)
	}
}

// 测试连接
// @Tags 数据源
// @Summary 测试连接
// @Security ApiKeyAuth
// @Param data body tool3.GenDatasourceAddReq true "测试连接"
// @Success 200 {object} response.Response{data=bool,msg=string} "{"code": 200, "data": [...]}"
// @Produce  application/json
// @Router /tool/codegen/datasource/testConnection [get]
func (d *GenDatasourceCtrl) TestConnection(c *gin.Context) {
	addReq := new(tool3.GenDatasourceAddReq)
	if err := c.ShouldBindQuery(addReq); err != nil {
		global.MoLogger.Info("参数绑定失败", zap.Error(err))
		response.FailMsgAsI18n(c, consts.ValidatorParamsCheckFailCode, consts.TKey_ValidatorParamsCheckFailMsg)
		return
	}
	if addReq.DbPasswd != "" {
		srcText, err := utils.Sm4Decrypt(utils.Trans_Sm4Key, addReq.DbPasswd)
		if err != nil {
			global.MoLogger.Error("数据源密码解密失败", zap.Error(err))
			response.FailMsgAsI18n(c, consts.ServerOccurredErrorCode, consts.TKey_DataSourcePasswdFailMsg)
			return
		}
		addReq.DbPasswd = srcText
	}
	if err := services.ServiceGroupInst.ToolGroupInst.GenDatasourceServiceInst.TestConnection(addReq); err != nil {
		global.MoLogger.Error("测试连接失败", zap.Error(err))
		response.FailErrorWithCodeAsI18n(c, err, consts.ServerOccurredErrorCode, consts.TKey_DataSourceTestConnFailMsg)
		return
	} else {
		response.SuccessAsI18n(c, consts.TKey_DataSourceTestConnSuccessMsg, nil)
	}
}
